package com.ruoyi.web.controller.workJob;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.worker.domain.TermTimeShift;
import com.ruoyi.worker.service.TermTimeShiftService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: TermTimeShiftController
 * @Description:
 * @Author: 刘松山
 * @Date: 2021/10/13 19:33
 */
@RestController
@RequestMapping("/workJobApi/termTimeShift")
public class TermTimeShiftController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TermTimeShiftController.class);
    @Autowired
    private TermTimeShiftService termTimeShiftService;


    // @PreAuthorize("@ss.hasPermi('job:attendanceGroup:list')")
    @GetMapping("/listTermTimeShift")
    public TableDataInfo listTermTimeShift(TermTimeShift termTimeShift) {
        startPage();
        List<TermTimeShift> list = termTimeShiftService.listTermTimeShift(termTimeShift);

        for (TermTimeShift t : list) {
            String showTime = "";
            if (t.getType() == 1) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime();
                t.setTypeName("一天1次上下班打卡");
            } else if (t.getType() == 2) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime()
                        + "第2次上下班： " + t.getSecondBeginTime() + "-" + t.getSecondEndTime();
                t.setTypeName("一天2次上下班打卡");
            } else if (t.getType() == 3) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime()
                        + "第2次上下班： " + t.getSecondBeginTime() + "-" + t.getSecondEndTime()
                        + "第3次上下班： " + t.getThreeBeginTime() + "-" + t.getThreeEndTime();
                t.setTypeName("一天3次上下班打卡");
            } else if (t.getType() == 4) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime()
                        + "第2次上下班： " + t.getSecondBeginTime() + "-" + t.getSecondEndTime()
                        + "第3次上下班： " + t.getThreeBeginTime() + "-" + t.getThreeEndTime()
                        + "第4次上下班： " + t.getFourBeginTime() + "-" + t.getFourEndTime();
                t.setTypeName("一天4次上下班打卡");
            }


            t.setShowDateTime(showTime);
        }
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('job:attendanceGroup:query')")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getTermTimeShift(@PathVariable(value = "id", required = false) Long id) {
        AjaxResult ajax = AjaxResult.success();

        if (StringUtils.isNotNull(id)) {
            ajax.put(AjaxResult.DATA_TAG, termTimeShiftService.getTermTimeShift(id));

        }
        return ajax;
    }

    //  @PreAuthorize("@ss.hasPermi('workJobApi:attendance:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addTermTimeShift(@Validated @RequestBody TermTimeShift termTimeShift) {

        termTimeShift.setCreateBy(getUsername());
        int flag = termTimeShiftService.addTermTimeShift(termTimeShift);
        if (flag < 0) {
            return AjaxResult.error(termTimeShift.getName() + "的班次已经存在了");
        }
        return toAjax(flag);
    }


    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TermTimeShift termTimeShift) {

        termTimeShift.setUpdateBy(getUsername());
        int flag = termTimeShiftService.modifTermTimeShift(termTimeShift);
        if (flag < 0) {
            return AjaxResult.error(termTimeShift.getName() + "的班次已经存在了");
        }
        return toAjax(flag);
    }

    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/modifTermTimeShiftStatus")
    public AjaxResult modifTermTimeShiftStatus(@Validated @RequestBody TermTimeShift termTimeShift) {

        termTimeShift.setUpdateBy(getUsername());
        int flag = termTimeShiftService.modifTermTimeShiftStatus(termTimeShift);

        return toAjax(flag);
    }

    /**
     * 删除
     */
    //@PreAuthorize("@ss.hasPermi('job:attendanceGroup:detail:delete')")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delBactchTermTimeShift(@PathVariable Long[] ids) {
        return toAjax(termTimeShiftService.delBactchTermTimeShift(ids));
    }
}
